package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dto.OrderSettingDto;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {


    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 日历查询数据
     *
     * @param
     * @return
     */
    @Override
    public List<OrderSettingDto> getOrderSettingByMonth(Integer year, Integer month) {
        //从数据库中查数据
        List<OrderSetting> orderSettings=   orderSettingDao.getOrderSettingByMonth(year,month);
        List<OrderSettingDto> list=new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            OrderSettingDto orderSettingDto = new OrderSettingDto();
            orderSettingDto.setDate(orderSetting.getOrderDate().getDate());
            orderSettingDto.setNumber(orderSetting.getNumber());
            orderSettingDto.setReservations(orderSetting.getReservations());
            list.add(orderSettingDto);
        }
        return list;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        int count = orderSettingDao.update(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingDao.updateBy(orderSetting);
        }else {
            orderSettingDao.add(orderSetting);
        }
    }


    @Override
    public void add(List<ArrayList<String>> sheetData) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (int i = 0; i < sheetData.size(); i++) {
            ArrayList<String> data = sheetData.get(i);
            OrderSetting orderSetting = new OrderSetting();
            try {
                orderSetting.setOrderDate(sdf.parse(data.get(0)));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            orderSetting.setNumber(Integer.parseInt(data.get(1)));

            orderSettingDao.add(orderSetting);
        }
    }

    @Override
    public OrderSetting selectByDate(Date orderDate) {
        log.info("[订单设置-根据日期查询订单设置]date:"+orderDate);
        OrderSetting orderSetting=orderSettingDao.selectByDate(orderDate);
        return orderSetting;
    }
}